<template>
  <!--  ---------------搜索工具条-----------------------  -->
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="学生姓名" prop="stuName">
        <el-input
          v-model="queryParams.stuName"
          placeholder="请输入学生姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="学生微信" prop="stuWx">
        <el-input
          v-model="queryParams.stuWx"
          placeholder="请输入学生微信"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="学生电话" prop="stuPhone">
        <el-input
          v-model="queryParams.stuPhone"
          placeholder="请输入学生电话"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="问题描述" prop="note">
        <el-input
          v-model="queryParams.note"
          placeholder="请输入问题描述"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="预约时间" prop="orderTime">
        <el-date-picker clearable
                        v-model="queryParams.orderTime"
                        type="date"
                        value-format="yyyy-MM-dd"
                        placeholder="请选择预约时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="完成时间" prop="finishTime">
        <el-date-picker clearable
                        v-model="queryParams.finishTime"
                        type="date"
                        value-format="yyyy-MM-dd"
                        placeholder="请选择完成时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="所在班级" prop="className">
        <el-select v-model="queryParams.className" placeholder="请选择所在班级" clearable>
          <el-option
            v-for="dict in dict.type.student_help_class"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="问题类型" prop="typeId">
        <el-select v-model="queryParams.typeId" placeholder="请选择问题类型" clearable>
          <el-option
            v-for="dict in dict.type.student_help_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="问题状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择问题状态" clearable>
          <el-option
            v-for="dict in dict.type.student_help_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="帮扶老师" prop="teacherId">
        <el-input
          v-model="queryParams.teacherId"
          placeholder="请输入帮扶老师"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="教师备注" prop="comment">
        <el-input
          v-model="queryParams.comment"
          placeholder="请输入教师备注"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:help:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:help:edit']"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:help:remove']"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:help:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>
    <!--  -----------------------列表信息展示---------------------------  -->
    <el-table v-loading="loading" :data="helpList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <!--      <el-table-column label="ID" align="center" prop="id" />-->
      <el-table-column label="学生姓名" align="center" prop="stuName"/>
      <el-table-column label="学生微信" width="80" align="center" prop="stuWx"/>
      <el-table-column label="学生电话" width="120" align="center" prop="stuPhone"/>
      <el-table-column label="所在班级" width="80" align="center" prop="className">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.student_help_class" :value="scope.row.className"/>
        </template>
      </el-table-column>
      <el-table-column label="问题描述" width="100" align="center" prop="note"/>
      <el-table-column label="预约时间" align="center" prop="orderTime" width="100">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.orderTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <!--      <el-table-column label="性别" width="60" align="center"  prop="sex">-->
      <!--        <template slot-scope="scope">{{ scope.row.sex === 1 ? '男' : '女' }}</template>-->
      <!--      </el-table-column>-->
      <el-table-column label="帮扶老师" align="center" prop="teacher.username"/>
      <el-table-column label="问题类型" align="center" prop="typeId">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.student_help_type" :value="scope.row.typeId"/>
        </template>
      </el-table-column>
      <el-table-column label="问题状态" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.student_help_status" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="完成时间" align="center" prop="finishTime" width="100">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.finishTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="教师备注" align="center" prop="comment"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:help:edit']"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:help:remove']"
          >删除
          </el-button>
          <el-button
            size="mini"
            type="primary"
            plain
            icon="el-icon-plus"
            @click="stuAdd"
            v-hasPermi="['system:evaluate:add']"
          >评价
          </el-button>
        </template>
      </el-table-column>
    </el-table>

<!--    <el-table v-loading="loading" :data="evaluateList" @selection-change="handleSelectionChange">-->
<!--      <el-table-column type="selection" width="55" align="center"/>-->
<!--      <el-table-column label="id" align="center" prop="id"/>-->
<!--      <el-table-column label="评价人" align="center" prop="student.stuName"/>-->
<!--      <el-table-column label="评价内容" align="center" prop="evaluate"/>-->
<!--      <el-table-column label="评价老师" align="center" prop="teacher.username"/>-->
<!--      <el-table-column label="分数" align="center" prop="score">-->
<!--        <template slot-scope="scope">-->
<!--          <dict-tag :options="dict.type.student_help_score" :value="scope.row.score"/>-->
<!--        </template>-->
<!--      </el-table-column>-->
<!--    </el-table>-->

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- ----------------------新增帮扶信息对话框 --------------------------------->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="学生姓名" prop="stuName">
          <el-input v-model="form.stuName" placeholder="请输入学生姓名"/>
        </el-form-item>
        <el-form-item label="学生微信" prop="stuWx">
          <el-input v-model="form.stuWx" placeholder="请输入学生微信"/>
        </el-form-item>
        <el-form-item label="学生电话" prop="stuPhone">
          <el-input v-model="form.stuPhone" placeholder="请输入学生电话"/>
        </el-form-item>

        <el-form-item label="预约时间" prop="orderTime">
          <el-date-picker clearable
                          v-model="form.orderTime"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="请选择预约时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="问题类型" prop="typeId">
          <el-select v-model="form.typeId" placeholder="请选择问题类型">
            <el-option
              v-for="dict in dict.type.student_help_type"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="帮扶状态" prop="status">
          <el-select v-model="form.status" placeholder="请选择帮扶状态">
            <el-option
              v-for="dict in dict.type.student_help_status"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="帮扶老师" prop="teacherId">
          <el-select v-model="form.teacherId" placeholder="请选择帮扶老师">
            <el-option
              v-for="item in teachers"
              :label="item.username"
              :value="item.id">
              <span style="float: left">{{ item.username }}</span>
              <span style="float: right; color: #8492a6; font-size: 13px">{{ item.phone }}</span>
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="所在班级" prop="className">
          <el-select v-model="form.className" placeholder="请选择所在班级" clearable>
            <el-option
              v-for="dict in dict.type.student_help_class"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="问题描述" prop="note">
          <el-input v-model="form.note" placeholder="请输入问题描述"/>
        </el-form-item>
        <!--        <el-form-item label="完成时间" prop="finishTime">-->
        <!--          <el-date-picker clearable-->
        <!--                          v-model="form.finishTime"-->
        <!--                          type="date"-->
        <!--                          value-format="yyyy-MM-dd"-->
        <!--                          placeholder="请选择完成时间">-->
        <!--          </el-date-picker>-->
        <!--        </el-form-item>-->
        <!--        <el-form-item label="教师备注" prop="comment">-->
        <!--          <el-input v-model="form.comment" placeholder="请输入教师备注" />-->
        <!--        </el-form-item>-->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- ----------------------修改帮扶信息对话框 --------------------------------->
    <el-dialog :title="title" :visible.sync="upOpen" width="500px" append-to-body>
      <el-form ref="upform" :model="upform" :rules="rules" label-width="80px">
        <el-form-item label="学生姓名" prop="stuName">
          <el-input v-model="upform.stuName" placeholder="请输入学生姓名" readonly="readonly"/>
        </el-form-item>
        <el-form-item label="学生微信" prop="stuWx">
          <el-input v-model="upform.stuWx" placeholder="请输入学生微信" readonly="readonly"/>
        </el-form-item>
        <el-form-item label="学生电话" prop="stuPhone">
          <el-input v-model="upform.stuPhone" placeholder="请输入学生电话" readonly="readonly"/>
        </el-form-item>

        <el-form-item label="预约时间" prop="orderTime">
          <el-date-picker clearable
                          v-model="upform .orderTime"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="请选择预约时间" readonly="readonly">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="问题类型" prop="typeId">
          <el-select v-model="upform.typeId" placeholder="请选择问题类型" disabled="disabled">
            <el-option
              v-for="dict in dict.type.student_help_type"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="帮扶老师" prop="teacherId">
          <el-select v-model="upform.teacherId" placeholder="请选择帮扶老师" disabled="disabled">
            <el-option
              v-for="item in teachers"
              :label="item.username"
              :value="item.id">
              <span style="float: left">{{ item.username }}</span>
              <span style="float: right; color: #8492a6; font-size: 13px">{{ item.phone }}</span>
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="所在班级" prop="className">
          <el-select v-model="upform.className" placeholder="请选择所在班级" disabled="disabled">
            <el-option
              v-for="dict in dict.type.student_help_class"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="问题描述" prop="note">
          <el-input v-model="upform.note" placeholder="请输入问题描述" disabled="disabled"/>
        </el-form-item>
        <el-form-item label="帮扶状态" prop="status">
          <el-select v-model="upform.status" placeholder="请选择帮扶状态">
            <el-option
              v-for="dict in dict.type.student_help_status"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="完成时间" prop="finishTime">
          <el-date-picker clearable
                          v-model="upform.finishTime"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="请选择完成时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="教师备注" prop="comment">
          <el-input v-model="upform.comment" placeholder="请输入教师备注"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm2">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 添加或修改评价管理对话框 -->
    <el-dialog :title="title" :visible.sync="stuOpen" width="500px" append-to-body>
      <el-form ref="stuForm" :model="stuForm" :rules="rules" label-width="80px">
        <el-form-item label="评价人" prop="studentId">
          <el-select v-model="stuForm.studentId" placeholder="请输入评价人">
            <el-option
              v-for="item in students"
              :label="item.stuName"
              :value="item.id">
              <span style="float: left">{{ item.stuName }}</span>
              <span style="float: right; color: #8492a6; font-size: 13px">{{ item.stuPhone }}</span>
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="评价老师" prop="studentId">
          <el-select v-model="stuForm.teacherId" placeholder="请输入评价老师">
            <el-option
              v-for="item in teachers"
              :label="item.username"
              :value="item.id">
              <span style="float: left">{{ item.username }}</span>
              <span style="float: right; color: #8492a6; font-size: 13px">{{ item.phone }}</span>
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="评价内容" prop="evaluate">
          <el-input v-model="stuForm.evaluate" placeholder="请输入评价内容"/>
        </el-form-item>
        <el-form-item label="分数" prop="score">
          <el-select v-model="stuForm.score" placeholder="请选择分数">
            <el-option
              v-for="dict in dict.type.student_help_score"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm3">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <el-drawer
      :title="title"
      :visible.sync="table"
      direction="rtl"
      size="50%">
      <el-table :data="gridData">
        <el-table-column property="id" label="id" width="150"></el-table-column>
        <el-table-column property="name" label="评价人" width="150"></el-table-column>
      </el-table>
    </el-drawer>
  </div>


</template>

<script>
import {listHelp, getHelp, delHelp, addHelp, updateHelp, getEvaluates} from "@/api/system/help";
import {listTeacher} from "@/api/system/teacher";
import {addEvaluate, listEvaluate, updateEvaluate} from "@/api/system/evaluate";

export default {
  name: "Help",
  dicts: ['student_help_type', 'student_help_status', 'student_help_class', 'student_help_score'],
  data() {
    return {
      table: false,
      gridData: [],
      // 评价列表
      evaluateList: [],
      // 学生
      students: [],
      // 帮扶教师
      teachers: [],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 帮扶信息表格数据
      helpList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      upOpen: false,
      stuOpen: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        stuName: null,
        stuWx: null,
        stuPhone: null,
        note: null,
        orderTime: null,
        sex: null,
        teacherId: null,
        typeId: null,
        status: null,
        className: null,
        finishTime: null,
        comment: null
      },
      // 查询参数
      queryParams2: {
        pageNum: 1,
        pageSize: 10,
        studentId: null,
        evaluate: null,
        score: null,
        teacherId: null
      },
      // 表单参数
      form: {},
      // 修改表单参数
      upform: {},
      stuForm: {},
      // 表单校验
      rules: {
        stuName: [
          {required: true, message: "学生姓名不能为空", trigger: "blur"}
        ],
        stuWx: [
          {required: true, message: "学生微信不能为空", trigger: "blur"}
        ],
        orderTime: [
          {required: true, message: "预约时间不能为空", trigger: "blur"}
        ],
        teacherId: [
          {required: true, message: "帮扶老师不能为空", trigger: "change"}
        ],
        typeId: [
          {required: true, message: "问题类型不能为空", trigger: "change"}
        ],
      }
    };
  },
  created() {
    this.getList();
    this.getTeachers();
    this.getStu();
    this.getEva();
  },
  methods: {
    // 抽屉数据，获取评价信息
    handleC(row) {
      this.title = "学生评价";
      this.table = true;
      let typeId = row.id;
      getEvaluates(typeId).then(response => {
        this.gridData = response.data;
      }).catch(response => {
        response.$message.error("网络故障，请稍后重试");
      })
    },
    /** 查询帮扶信息列表 */
    getList() {
      this.loading = true;
      listHelp(this.queryParams).then(response => {
        this.helpList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 查询帮扶老师列表 */
    getTeachers() {
      this.loading = true;
      listTeacher(this.queryParams).then(response => {
        this.teachers = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    getEva() {
      this.loading = true;
      listEvaluate(this.queryParams2).then(response => {
        this.evaluateList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    getStu() {
      this.loading = true;
      listHelp(this.queryParams).then(response => {
        this.students = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.upOpen = false;
      this.reset();
      this.reset2();
      this.reset3();
      this.stuOpen = false;
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        stuName: null,
        stuWx: null,
        stuPhone: null,
        note: null,
        orderTime: null,
        sex: null,
        teacherId: null,
        typeId: null,
        status: null,
        className: null,
        finishTime: null,
        comment: null
      };
      this.resetForm("form");
    },
    reset2() {
      this.form = {
        id: null,
        stuName: null,
        stuWx: null,
        stuPhone: null,
        note: null,
        orderTime: null,
        sex: null,
        teacherId: null,
        typeId: null,
        status: null,
        className: null,
        finishTime: null,
        comment: null
      };
      this.resetForm("upForm");
    },
    // 表单重置
    reset3() {
      this.stuForm = {
        id: null,
        studentId: null,
        evaluate: null,
        score: null
      };
      this.resetForm("stuForm");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 评价新增按钮操作 */
    stuAdd() {
      this.reset3();
      this.stuOpen = true;
      this.title = "添加评价";
      this.getStu();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.reset2();
      this.open = true;
      this.title = "添加帮扶信息";
      this.getTeachers();

    },

    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getHelp(id).then(response => {
        this.upform = response.data;
        this.upOpen = true;
        this.title = "修改帮扶信息";
      });
      this.getTeachers();
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateHelp(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.upOpen = false;
              this.getList();
            });
          } else {
            addHelp(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    submitForm2() {
      this.$refs["upform"].validate(valid => {
        if (valid) {
          if (this.upform.id != null) {
            updateHelp(this.upform).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.upOpen = false;
              this.getList();
            });
          } /*else {
            addStudent(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }*/
        }
      });
    },
    submitForm3() {
      this.$refs["stuForm"].validate(valid => {
        if (valid) {
          if (this.stuForm.id != null) {
            updateEvaluate(this.stuForm).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.stuOpen = false;
              this.getEva();
            });
          } else {
            addEvaluate(this.stuForm).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.stuOpen = false;
              this.getEva();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除帮扶信息编号为"' + ids + '"的数据项？').then(function () {
        return delHelp(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/help/export', {
        ...this.queryParams
      }, `help_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
